/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: GameToggleButton.java 563 2010-07-25 20:54:40Z Harry $
 *   $LastChangedDate: 2010-07-25 20:54:40 +0000 (Sun, 25 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.component;

import battlefield.resource.Sounds;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JToggleButton;

/**
 *
 * @author Harry Nguyen
 */
public class GameToggleButton extends JToggleButton implements MouseListener {

    private boolean hover;
    private Image normalImage;
    private Image hoverImage;

    public GameToggleButton() {
        initComponents();
    }

    private void initComponents() {
        this.setFocusPainted(false);
        this.setBorderPainted(false);
        this.setContentAreaFilled(false);
        this.setMargin(new Insets(0, 0, 0, 0));
        this.setOpaque(false);
        this.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        this.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        this.addMouseListener(this);
    }

    public boolean isHover() {
        return hover;
    }

    public void setHover(boolean hover) {
        this.hover = hover;
    }

    public Image getNormalImage() {
        return normalImage;
    }

    public void setNormalImage(Image normalImage) {
        this.normalImage = normalImage;
    }

    public Image getHoverImage() {
        return hoverImage;
    }

    public void setHoverImage(Image hoverImage) {
        this.hoverImage = hoverImage;
    }

    @Override
    public void paintComponent(java.awt.Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, this.isSelected() ? 1f : 0.6f));
        Image image = isHover() ? hoverImage : normalImage;
        if (image != null) {
            g2d.drawImage(image, 0, 0, this);
        }
        super.paintComponent(g2d);
        g2d.dispose();
    }

    public void mouseClicked(MouseEvent e) {
        Sounds.MOUSE_CLICK.play();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        setHover(true);
        repaint();
        Sounds.MOUSE_HOVER.play();
    }

    public void mouseExited(MouseEvent e) {
        setHover(false);
        repaint();
    }
}
